package com.pignest.hotel.ui.reception;

import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;

import com.pignest.framework.util.Log;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.definition.OpenRoomType;
import com.pignest.hotel.definition.RoomLiveStatus;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Config;
import com.pignest.hotel.system.InComeUtil;
import com.pignest.hotel.system.Logic;
import com.pignest.hotel.ui.income.BaseCheckOutWindow;
import com.pignest.hotel.ui.room.RoomHistogram;
import com.pignest.hotel.ui.room.RoomView;
import com.pignest.hotel.util.UIUtils;

/**
 * 接待房态图
 * 
 * @author Jude
 */
public class ReceptionHistogram extends RoomHistogram {
	private static final long serialVersionUID = 7853613299861470078L;

	public ReceptionHistogram() {
		super();
		setLineCapacity(Config.getLineCapacityOfReception());
		setShowFlags(RoomView.SHOW_ALL);
		setSelectable(false);
	}
	
	@Override
	protected void addEventListener(RoomView rv) {
		rv.addEventListener(Events.ON_DOUBLE_CLICK, this);
	}
	
	@Override
	public void onEvent(Event event) throws Exception {
		super.onEvent(event);
		if (Events.ON_DOUBLE_CLICK.equals(event.getName())) {
			Component c = (RoomView)event.getTarget();
			if (!(c instanceof RoomView))
				return;
			RoomView rv = (RoomView)c;
			setSelected(rv);
			Room room = Services.getRoomService().getRoom(rv.getRoomId());
			if (RoomLiveStatus.FILLED.is(room)) {
				onCheckout(room);
			} else if (Logic.canLiving(room)) {
				onLiving(room);
			}
		}
	}
	
	public void remarkRoom(String roomId) {
		final RemarkRoomWindow win = (RemarkRoomWindow) Executions.createComponents(
				"/modules/reception/remarkWindow.zul", null, null);
		win.setClosable(true);
		win.setRoomId(roomId);
		try {
			win.doModal();
			if (win.isSuccess()) {
				RoomView rv = getView(roomId);
				if (rv != null)
					rv.updateRoom();
			}
		} catch (Exception e) {
		}
	}
	
	public void changeRoom(String roomId) {
		Room room = Services.getRoomService().getRoom(roomId);
		if (room == null)
			return;

		final ChangeRoomWindow win = (ChangeRoomWindow) Executions.createComponents(
				"/modules/reception/changeRoomWindow.zul", null, null);
		win.setClosable(true);
		win.setRoom(room);
		try {
			win.doModal();
			if (win.isSuccess()) {
				refresh();
			}
		} catch (Exception e) {
		}
	}
	
	public void onLiving(String roomId) {
		Room room = Services.getRoomService().getRoom(roomId);
		if (Logic.canLiving(room))
			onLiving(room);
	}
	
	/** 入住 */
	private void onLiving(Room room) {
		final LivingWindow win = (LivingWindow) Executions.createComponents(
				"/modules/reception/livingWindow.zul", null, null);
		win.setClosable(true);
		win.setRoom(room);
		try {
			win.doModal();
			if (win.isSuccess()) {
				RoomView rv = getView(room.getId());
				if (rv != null)
					rv.updateRoom();
			}
		} catch (Exception e) {
			Log.e(e);
		}
	}
	
	public void onCheckout(String roomId) {
		Room room = Services.getRoomService().getRoom(roomId);
		if (RoomLiveStatus.FILLED.is(room))
			onCheckout(room);
	}
	
	/** 退房 */
	private void onCheckout(Room room) {
		ReceptionService receptionService = Services.getReceptionService();
		List<Living> l =receptionService.getLivingList(room);
		String url="/modules/income/inComeCheckOutWindow.zul";
		if(InComeUtil.isNoSameByOpenRoomState(l,OpenRoomType.DAY.name())){
			url="/modules/income/inComeCheckOutWindow.zul";
		}else if(InComeUtil.isNoSameByOpenRoomState(l,OpenRoomType.HALFDAY.name())){
			url="/modules/income/inComeHDCheckOutWindow.zul";
		}else if(InComeUtil.isNoSameByOpenRoomState(l,OpenRoomType.HOURS.name())){
			url="/modules/income/inComeHCheckOutWindow.zul";
		}else{
			UIUtils.alert("记录中不同的开房类型!请同一开房类型");
			return ;
		}
		final BaseCheckOutWindow win = (BaseCheckOutWindow) Executions.createComponents(
				url, null, null);
		win.setClosable(true);
		win.setSelectLive(l);
		try {
			win.doModal();
			if (win.isSuccess()) {
				RoomView rv = getView(room.getId());
				if (rv != null)
					rv.updateRoom();
			}
		} catch (Exception e) {
			Log.e(e);
		}
	}

	/**修改入住信息*/
	public void onModLiving(Room room){
		ReceptionService receptionService = Services.getReceptionService();
		Living l =receptionService.getLiving(room);
		String url="/modules/reception/modLivingWindow.zul";
		final ModLivingWindow win =(ModLivingWindow)Executions.createComponents(
				url, null, null);
		win.setClosable(true);
		win.setCurLiving(l);
		try {
			win.doModal();
			if(win.isSuccess()){
		      refresh();
			}
		}catch(Exception e) {
			Log.e(e);
		}
	}
}
